﻿package mortal.game.view.wizard.newPanel
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import fl.data.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.display.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.wizard.data.*;
    import mortal.game.view.wizard.panel.*;
    import mortal.mvc.core.*;

    public class WizardNewInfoPanel extends WizardBasePanel
    {
        private var _currentWizardData:WizardData;
        private var _isUseGold:Boolean = true;
        private var _bottomPart:WizardSkillPart;
        private var _equipPart:WizardEquipPart;
        private var _rightPart:WizardRightPart;
        private var _tabBar:GTileList;
        private var _roleBg:GImageBitmap;
        protected var _img2d:Img2D;
        protected var _rect3d:Rect3DObject;
        protected var _bodyPlayer:WizardActionPlayer;
        private var _3dRotationSp:GSprite;
        private var _3dRotationBm:GBitmap;
        private var _effectPlayer:EffectPlayer;
        private var _starUpEffectPlayer:EffectPlayer;
        private var _window:Window;
        private var timerContorler:TimeControler;
        private var _showTween:TweenMax;
        private var _extShowTween:TweenMax;
        private var _hideTween:TweenMax;
        private var _extHideTween:TweenMax;
        private var _offsetX:Number;
        private var _lastWizardModel:TModel;
        private var _lastBodyPlayer:WizardActionPlayer;
        private var _effectVec:Vector.<EffectPlayer>;
        private var _startX:Number;
        private var _endX:Number;
        private var _endY:Number;
        private var _baseDelta:int = 3;
        private var _offsetY:int;
        private var _effectEquip:OtherEquipItem;
        private var _effectSkill:WizardSkillItem;
        public static const starPanel:int = 1;
        public static const levelUpItemtxt:int = 2;
        public static const upgradeBtn:int = 3;
        public static const consumeBox2:int = 4;

        public function WizardNewInfoPanel(param1:Window)
        {
            this._effectVec = new Vector.<EffectPlayer>;
            this._window = param1;
            this.timerContorler = TimeControler.createTimerInstance();
            this.timerContorler.unActive();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._roleBg = UIFactory.gImageBitmap(ImagesConst.WizardRoleBg, 0, 0, null, this.addImg2d);
            this._3dRotationSp = UICompomentPool.getUICompoment(GSprite);
            this._3dRotationSp.mouseEnabled = true;
            this._window.contentTopOf3DSprite.addChild(this._3dRotationSp);
            this._3dRotationBm = GlobalClass.getBitmap(BitmapDataConst.AlphaBMD);
            this._3dRotationBm.x = 12;
            this._3dRotationBm.y = 45;
            this._3dRotationBm.width = 598;
            this._3dRotationBm.height = 545;
            this._3dRotationSp.addChild(this._3dRotationBm);
            this.pushUIToDisposeVec(UIFactory.bg(3, 33, 608, 554, this, ImagesConst.MountWinBg));
            this.pushUIToDisposeVec(UIFactory.bg(611, 33, 265, 554, this, ImagesConst.MountWinBg));
            this._equipPart = UICompomentPool.getUICompoment(WizardEquipPart);
            this._equipPart.move(29, 383);
            this._window.contentTopOf3DSprite.addChild(this._equipPart);
            this._tabBar = UIFactory.tileList(-88, 60, 170, 700, this._window.contentTopOf3DSprite);
            this._tabBar.columnWidth = 90;
            this._tabBar.rowHeight = 43;
            this._tabBar.horizontalGap = 0;
            this._tabBar.verticalGap = -6;
            this._tabBar.setStyle("cellRenderer", WizardNewTabRender);
            this._tabBar.configEventListener(Event.CHANGE, this.tabBarChangeHandler);
            this._tabBar.dataProvider = this.getDateProvider();
            this._tabBar.selectedIndex = -1;
            this._bottomPart = UICompomentPool.getUICompoment(WizardSkillPart);
            this._bottomPart.x = 118;
            this._bottomPart.y = 440;
            this._window.contentTopOf3DSprite.addChild(this._bottomPart);
            this._rightPart = UICompomentPool.getUICompoment(WizardRightPart);
            this._rightPart.x = 615;
            this._rightPart.y = 38;
            this._window.contentTopOf3DSprite.addChild(this._rightPart);
            this.playTransferEffect();
            if (Cache.instance.wizard.fightWizardCode)
            {
                this.showSelectWizard((Cache.instance.wizard.fightWizardCode - 1));
            }
            else
            {
                this.showSelectWizard(0);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._3dRotationBm.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.startTurn);
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.stopTurn);
            this.remove3d();
            this.removeEffect();
            if (this._img2d)
            {
                this._img2d.dispose(param1);
                this._img2d = null;
            }
            if (this._bodyPlayer)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._rect3d.disposeObj3d(this._bodyPlayer);
                this._bodyPlayer = null;
            }
            if (this._lastBodyPlayer)
            {
                this._rect3d.disposeObj3d(this._lastBodyPlayer);
                this._lastBodyPlayer = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d.disposeObj3d(this._effectPlayer);
                this._effectPlayer = null;
                this._rect3d = null;
                this._img2d = null;
            }
            this.timerContorler.unActive();
            this._roleBg.dispose(param1);
            this._roleBg = null;
            this._tabBar.dispose(param1);
            this._tabBar = null;
            this._bottomPart.dispose(param1);
            this._bottomPart = null;
            this._rightPart.dispose(param1);
            this._rightPart = null;
            this._3dRotationSp.dispose(param1);
            this._3dRotationSp = null;
            this._3dRotationBm.dispose(param1);
            this._3dRotationBm = null;
            this._equipPart.dispose(param1);
            this._equipPart = null;
            this._effectEquip = null;
            this._effectSkill = null;
            this._lastWizardModel = null;
            this._currentWizardData = null;
            if (this._showTween)
            {
                this._showTween.kill();
                this._showTween = null;
            }
            if (this._hideTween)
            {
                this._hideTween.kill();
                this._hideTween = null;
            }
            if (this._extShowTween)
            {
                this._extShowTween.kill();
                this._extShowTween = null;
            }
            if (this._extHideTween)
            {
                this._extHideTween.kill();
                this._extHideTween = null;
            }
            super.disposeImpl(param1);
            return;
        }// end function

        private function startTurn(event:MouseEvent) : void
        {
            this._startX = this.mouseX;
            this._3dRotationSp.configEventListener(MouseEvent.MOUSE_MOVE, this.turning);
            return;
        }// end function

        private function stopTurn(event:MouseEvent) : void
        {
            this._3dRotationSp.removeEventListener(MouseEvent.MOUSE_MOVE, this.turning);
            return;
        }// end function

        private function turning(event:MouseEvent) : void
        {
            this._endX = this.mouseX;
            if (this._bodyPlayer)
            {
                this._bodyPlayer.rotationY = this._bodyPlayer.rotationY - (this._endX - this._startX) / 3;
            }
            this._startX = this._endX;
            return;
        }// end function

        private function moduleScale(event:MouseEvent) : void
        {
            if (this._bodyPlayer.scaleValue + event.delta / 10 > 5)
            {
                this._bodyPlayer.scaleValue = 5;
            }
            else if (this._bodyPlayer.scaleValue + event.delta / 10 < 1)
            {
                this._bodyPlayer.scaleValue = 1;
            }
            else
            {
                this._bodyPlayer.scaleValue = this._bodyPlayer.scaleValue + event.delta / 10;
                this._bodyPlayer.y = this._bodyPlayer.y - 13 * event.delta;
            }
            return;
        }// end function

        private function addImg2d() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this.add3d();
            if (this._img2d == null)
            {
                this._img2d = new Img2D(null, this._roleBg.bitmapData, new Rectangle(0, 0, 598, 545), false);
                this._rect3d.addImg(this._img2d);
            }
            if (this._window != null)
            {
                this._window.dispatchEvent(new WindowEvent(WindowEvent.SHOW));
                Dispatcher.dispatchEvent(new DataEvent(EventName.WindowShowed, this._window));
            }
            this.update3dModel();
            this._3dRotationBm.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.startTurn);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopTurn);
            return;
        }// end function

        protected function add3d() : void
        {
            if (!this._rect3d)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(8, 38, 598, 545), this._window);
            }
            Rect3DManager.instance.windowShowHander(null, this._window);
            this._window.addEventListener(WindowEvent.POSITIONCHANGE, this.onWindowDragEnd);
            return;
        }// end function

        protected function update3dModel() : void
        {
            var showAction:Function;
            var model:TModel;
            var meshUrl:String;
            var boneUrl:String;
            var isFirstOpen:Boolean;
            showAction = function () : void
            {
                setActionName(ActionName.Standing);
                addEffect("effect1");
                return;
            }// end function
            ;
            if (this._rect3d && this._currentWizardData)
            {
                if (this._lastWizardModel)
                {
                    this.hideBodyPLayer();
                }
                model = ModelConfig.instance.getInfoByCode(int(this._currentWizardData.tSprite.modelId));
                meshUrl = model.mesh1 + ".md5mesh";
                boneUrl = model.bone1 + ".skeleton";
                if (this._bodyPlayer == null)
                {
                    this._bodyPlayer = FrObjectPool.getObject(WizardActionPlayer, this);
                    FrEventDispatcher.instance.proxyAddEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                    this._bodyPlayer.timerContorler = this.timerContorler;
                }
                this.timerContorler.active();
                isFirstOpen = this._lastWizardModel == null;
                this._bodyPlayer.clearExtend();
                this._bodyPlayer.load(meshUrl, boneUrl, model.texture1, this._rect3d.renderList);
                if (model.mesh2)
                {
                    meshUrl = model.mesh2 + ".md5mesh";
                    boneUrl = model.bone2 + ".skeleton";
                    this._bodyPlayer.loadExtend(meshUrl, boneUrl, model.texture2, this._rect3d.renderList);
                    this._bodyPlayer.extendPlayer.timerContorler = this.timerContorler;
                }
                this._bodyPlayer.play();
                this._bodyPlayer.rotationY = 0;
                this._lastWizardModel = model;
                if (isFirstOpen)
                {
                    this._bodyPlayer.scaleValue = 3;
                    this._rect3d.addObject3d(this._bodyPlayer, 300, 515, false);
                    this._offsetX = this._bodyPlayer.x - this._rect3d.rect.x;
                    this._offsetY = this._bodyPlayer.y + this._rect3d.rect.y;
                    this.setActionName(ActionName.Standing);
                    this.addEffect("effect1");
                }
                else
                {
                    this._bodyPlayer.scaleValue = 1.5;
                    this.setActionName(ActionName.Stand);
                    this._bodyPlayer.alpha = 0;
                    this._rect3d.addObject3d(this._bodyPlayer, 0, 0, false);
                    this._bodyPlayer.x = this.modelX - 380;
                    this._bodyPlayer.y = this.modelY + 80;
                    this._showTween = TweenMax.to(this._bodyPlayer, 1.5, {x:this.modelX, y:this.modelY, alpha:1, scaleValue:3, ease:Quint.easeOut, frameInterval:1, onComplete:showAction});
                    this.removeEffect();
                }
            }
            return;
        }// end function

        private function get modelX() : int
        {
            return this._offsetX + this._rect3d.rect.x;
        }// end function

        private function get modelY() : int
        {
            return this._offsetY - this._rect3d.rect.y;
        }// end function

        private function onWindowDragEnd(event:Event) : void
        {
            if (this._showTween)
            {
                this._showTween.kill();
                this._showTween = null;
            }
            if (this._hideTween)
            {
                this._hideTween.kill();
                this._hideTween = null;
            }
            this.removeEffect();
            this.removeLastBody();
            if (this._rect3d)
            {
                this._rect3d.addObject3d(this._bodyPlayer, 300, 515, false);
            }
            return;
        }// end function

        private function hideBodyPLayer() : void
        {
            this.removeEffect();
            this.removeLastBody();
            this._lastBodyPlayer = this._bodyPlayer;
            this._lastBodyPlayer.alpha = 1;
            this._hideTween = TweenMax.to(this._lastBodyPlayer, 0.8, {x:this.modelX + 380, y:this.modelY + 80, alpha:0, scaleValue:1.5, ease:Quint.easeOut, frameInterval:1, onComplete:this.removeLastBody});
            this._bodyPlayer = null;
            return;
        }// end function

        private function removeLastBody() : void
        {
            if (this._lastBodyPlayer)
            {
                this._rect3d.disposeObj3d(this._lastBodyPlayer);
                this._lastBodyPlayer = null;
                this.timerContorler.active();
            }
            return;
        }// end function

        private function addEffect(param1:String) : void
        {
            var _loc_3:Vector.<Array> = null;
            var _loc_4:Array = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:EffectPlayer = null;
            this.removeEffect();
            var _loc_2:* = ModelConfig.instance.getInfoByCode(int(this._currentWizardData.tSprite.modelId));
            if (_loc_2.effect1)
            {
                _loc_3 = GameStrUtil.alzToArray(_loc_2[param1]);
                _loc_4 = _loc_3[0];
                _loc_5 = _loc_3[1];
                _loc_6 = 0;
                while (_loc_6 < _loc_4.length)
                {
                    
                    _loc_7 = EffectPlayerPool.instance.getEffectPlayer(_loc_4[_loc_6], null, this._rect3d.renderList, false);
                    if (_loc_7.temple is TempleRole)
                    {
                        (_loc_7.temple as TempleRole).setRoleParams(this._bodyPlayer, null, null);
                    }
                    else
                    {
                        _loc_7.hangBoneName = _loc_5[_loc_6];
                        this._bodyPlayer.hang(_loc_7);
                    }
                    _loc_7.play(PlayMode.ANIMATION_LOOP_MODE);
                    this._effectVec.push(_loc_7);
                    _loc_6++;
                }
            }
            return;
        }// end function

        protected function onInitBodyPlayer(event:Event) : void
        {
            if (this._bodyPlayer)
            {
                this._bodyPlayer.addFrameComplete(ActionName.Standing, this.onFrameComplete);
            }
            return;
        }// end function

        private function onFrameComplete() : void
        {
            if (!this._bodyPlayer)
            {
                return;
            }
            this.setActionName(ActionName.Stand);
            this.addEffect("hangModel1");
            return;
        }// end function

        private function removeEffect() : void
        {
            var _loc_1:EffectPlayer = null;
            for each (_loc_1 in this._effectVec)
            {
                
                _loc_1.dispose(true);
            }
            this._effectVec.length = 0;
            return;
        }// end function

        public function setActionName(param1:String) : void
        {
            this._bodyPlayer.changeAction(param1);
            return;
        }// end function

        private function setAttrData() : void
        {
            this._rightPart.setAttrData();
            return;
        }// end function

        private function getDateProvider() : DataProvider
        {
            var _loc_3:int = 0;
            var _loc_4:WizardData = null;
            var _loc_5:Object = null;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.wizard.soulList;
            if (Global.isDebugModle)
            {
                _loc_3 = 8;
            }
            else
            {
                _loc_3 = 4;
            }
            for each (_loc_4 in _loc_2)
            {
                
                if (_loc_4.code <= _loc_3)
                {
                    _loc_5 = {data:_loc_4};
                    _loc_1.addItem(_loc_5);
                }
            }
            return _loc_1;
        }// end function

        private function tabBarChangeHandler(event:Event = null) : void
        {
            this.data = this._tabBar.selectedItem.data as WizardData;
            return;
        }// end function

        private function hideLevelUp(event:MouseEvent) : void
        {
            if (this._effectEquip)
            {
                EffectManager.glowFilterUnReg(this._effectEquip);
                if (event.currentTarget.visible)
                {
                    this._effectEquip.enabled = false;
                }
            }
            if (this._effectSkill)
            {
                EffectManager.glowFilterUnReg(this._effectSkill);
                if (event.currentTarget.visible)
                {
                    this._effectSkill.enabled = false;
                }
            }
            return;
        }// end function

        private function set data(param1:WizardData) : void
        {
            this._currentWizardData = param1;
            this._bottomPart.data = param1;
            this._equipPart.data = param1;
            this._rightPart.data = param1;
            this.update3dModel();
            this.refreshSprite();
            this.setAttrData();
            return;
        }// end function

        public function showSelectWizard(param1:int) : void
        {
            this._tabBar.dataProvider = this.getDateProvider();
            if (this._tabBar.selectedIndex == param1)
            {
                this.refreshSprite();
            }
            else
            {
                this._tabBar.selectedIndex = param1;
                this.tabBarChangeHandler();
            }
            return;
        }// end function

        public function updateEquipList() : void
        {
            this._equipPart.updateEquipList();
            return;
        }// end function

        public function updateAttrData() : void
        {
            this._rightPart.updateAttrData();
            return;
        }// end function

        public function updateSkill(param1:Boolean = true) : void
        {
            this._bottomPart.updateSkill(param1);
            return;
        }// end function

        public function updateCombat() : void
        {
            this._rightPart.updateCombat();
            return;
        }// end function

        public function refreshSprite() : void
        {
            this._bottomPart.refreshSprite();
            this._rightPart.refreshSprite();
            this.updateEquipList();
            this.updateSkill(false);
            return;
        }// end function

        public function updateItemHandler() : void
        {
            this._bottomPart.updateItemHandler();
            return;
        }// end function

        public function updateMoney() : void
        {
            this._rightPart.updateMoney();
            return;
        }// end function

        public function playEffect(param1:int) : void
        {
            var _loc_2:String = null;
            var _loc_3:Point = null;
            switch(param1)
            {
                case ESpriteUpdateType._ESpriteUpdateAdd:
                {
                    _loc_2 = "FSFB01_0301_dimian";
                    _loc_3 = new Point(this._bottomPart.x + 193, this._bottomPart.y + 25);
                    break;
                }
                case ESpriteUpdateType._ESpriteUpdateAcupointLevelUp:
                {
                    _loc_2 = "tuteng_2_zi";
                    _loc_3 = new Point(this._bottomPart.x + 190, this._bottomPart.y - 20);
                    break;
                }
                case ESpriteUpdateType._ESpriteUpdateChange:
                {
                    _loc_2 = "FSFB01_BOSS02_02";
                    _loc_3 = new Point(this._bottomPart.x + 170, this._bottomPart.y + 10);
                    break;
                }
                case ESpriteUpdateType._ESpriteUpdateSkillCode:
                {
                    _loc_2 = "tuteng_3_zi";
                    _loc_3 = new Point(this._bottomPart.x + 190, this._bottomPart.y - 210);
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this._rect3d && _loc_2 && _loc_3)
            {
                if (this._effectPlayer)
                {
                    this._rect3d.disposeObj3d(this._effectPlayer);
                }
                this._effectPlayer = EffectPlayerPool.instance.getEffectPlayer(_loc_2, null, this._rect3d.renderList, false);
                this._effectPlayer.play(PlayMode.ANIMATION_STOP_MODE);
                FrEventDispatcher.instance.proxyAddEventListener(this._effectPlayer, Engine3dEventName.PLAYEND, this.playEndHander);
                this._rect3d.addObject3d(this._effectPlayer, _loc_3.x, _loc_3.y);
            }
            return;
        }// end function

        private function playEndHander(event:Event) : void
        {
            var _loc_2:* = event.currentTarget as EffectPlayer;
            if (this._rect3d)
            {
                this._rect3d.disposeObj3d(_loc_2);
            }
            return;
        }// end function

        private function playTransferEffect() : void
        {
            if (this._rect3d)
            {
                if (this._starUpEffectPlayer)
                {
                    this._rect3d.disposeObj3d(this._starUpEffectPlayer);
                }
                this._starUpEffectPlayer = EffectPlayerPool.instance.getEffectPlayer("UI_test02", null, this._rect3d.renderList, false);
                this._starUpEffectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
                this._rect3d.addObject3d(this._starUpEffectPlayer, this._bottomPart.x + this._bottomPart.starUpBtn.x + 40, this._bottomPart.y + this._bottomPart.starUpBtn.y + 35);
            }
            return;
        }// end function

        private function remove3d() : void
        {
            if (this._rect3d)
            {
                this._rect3d.disposeObj3d(this._effectPlayer);
                this._effectPlayer = null;
                this._rect3d.disposeObj3d(this._starUpEffectPlayer);
                this._starUpEffectPlayer = null;
            }
            return;
        }// end function

        public function showFilter(param1:int) : void
        {
            switch(param1)
            {
                case starPanel:
                {
                    this._bottomPart.showFilter(param1);
                    break;
                }
                case levelUpItemtxt:
                {
                    this._bottomPart.showFilter(param1);
                    break;
                }
                case upgradeBtn:
                {
                    this._rightPart.showFilter(param1);
                    break;
                }
                case consumeBox2:
                {
                    this._rightPart.showFilter(param1);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

    }
}
